# -*- coding: utf-8 -*-
# Copyright 2011-2012 MagiCycles.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from datastore.common import BIKE_TYPE

from datastore.models import DBBike, DBStatistics

class Bike(object):
    """ A Bike"""
    DBTYPE = DBBike
    Types = BIKE_TYPE

    def __init__(self, dbbike=None):
        self.dbbike = dbbike

    def __getattr__(self, name):
        if name == "dbbike":
            return None
        return getattr(self.dbbike, name)

    @property
    def Stats(self):
        return DBStatistics.get_by_bike(self.dbbike)

    @classmethod
    def create(cls, siteuser, type, make, model, year, photourl,
               frame="", fork="", wheels="", components="", description=""):
        dbbike = DBBike.new_bike(type=type, make=make, model=model, year=year, owner=siteuser.dbuser, photourl=photourl,
            frame=frame, fork=fork, wheels=wheels, components=components, description=description)

        bike = cls(dbbike=dbbike)
        if dbbike:
            from people import Activity

            DBStatistics.add_bike(siteuser, dbbike)
            Activity.add_bike(siteuser, bike)

        return bike

    @classmethod
    def remove(cls, bike):
        from people import SiteUser

        siteuser = SiteUser.get_by_uid(bike.dbbike.owner.uid)
        DBBike.remove_dbbike(bike.dbbike)
        DBStatistics.remove_bike(siteuser)
        #Activity.remove_bike(siteuser, bike)

    @classmethod
    def get_bikes_by_user(cls, siteuser):
        return siteuser.Bikes

    @classmethod
    def get_by_id(cls, id):
        dbbike = DBBike.get_by_id(id)
        return dbbike and cls(dbbike)


def main():
    pass

if __name__ == '__main__':
    main()
